package com.example.myapplication;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class Tab2 extends Fragment {


    private RecyclerView recyclerView;

    private List<String> list;

    private Context context;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tab2, container, false);

        Context context =this.getActivity();

        recyclerView = view.findViewById(R.id.friendList);

        list = new ArrayList<>();
        for (int i=0;i<50;i++){
            list.add("测试人员"+i);
        }

        RecycleListAdapter adapter = new RecycleListAdapter(context, list);

        LinearLayoutManager manager = new LinearLayoutManager(context);

        manager.setOrientation(LinearLayoutManager.VERTICAL);

        recyclerView.setLayoutManager(manager);

        //这里可以定制,也有去掉最后一个分割线的方法
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
                manager.getOrientation());
        recyclerView.addItemDecoration(dividerItemDecoration);

        recyclerView.setAdapter(adapter);

        // 建立GroupItem文件后，inflate我的布局
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View groupView = layoutInflater.inflate(R.layout.item,null);

        // 调用recyclerView.addItemDecoration添加GroupItemDecoration
        recyclerView.addItemDecoration(new GroupItemDecoration(context,groupView,new GroupItemDecoration.DecorationCallback() {
            @Override
            public void setGroup(List<GroupItem> groupList) {
                //设置分组，GroupItem(int startPosition)，例如：
                GroupItem groupItem = new GroupItem(0);
                groupItem.setData("name","A");
                groupList.add(groupItem);

                groupItem = new GroupItem(5);
                groupItem.setData("name","B");
                groupList.add(groupItem);

                groupItem = new GroupItem(10);
                groupItem.setData("name","C");
                groupList.add(groupItem);

                groupItem = new GroupItem(15);
                groupItem.setData("name","D");
                groupList.add(groupItem);

                groupItem = new GroupItem(20);
                groupItem.setData("name","E");
                groupList.add(groupItem);

                groupItem = new GroupItem(25);
                groupItem.setData("name","F");
                groupList.add(groupItem);

                groupItem = new GroupItem(30);
                groupItem.setData("name","G");
                groupList.add(groupItem);
            }

            @Override
            public void buildGroupView(View groupView, GroupItem groupItem) {
                //构建groupView，通过groupView.findViewById找到内部控件（暂不支持点击事件等），例如
                TextView textName = (TextView) groupView.findViewById(R.id.listItemTextView);
                textName.setText(groupItem.getData("name").toString());
            }
        }));


        adapter.setOnItemClickListener(new RecycleListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView parent, View view, int position, String data) {
                Log.d("click",position+"");
                // 创建Intent，从列表页面跳转到测试人员详情页
                Intent intent = new Intent(getActivity(),friend_detail_page.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("index",position);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        // Inflate the layout for this fragment
        return view;
    }
}